package cn.android666.audiorecorder.pcm

import android.text.format.DateFormat
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel

class PCMSaver {

    private val fileName = "${DateFormat.format("HHmmss", System.currentTimeMillis())}.pcm"
    private val audioFile = File("/sdcard/", fileName)
    private var outputStream: FileOutputStream = FileOutputStream(audioFile)
    private var outputChannel: FileChannel = outputStream.channel

    fun close() {
        outputStream.close()
        Timber.i("录音结束，pcm文件位置：${audioFile.absolutePath}")
    }

    fun write(pcmByteBuffer: ByteBuffer) {
        outputChannel.write(pcmByteBuffer)
    }

}